<template>
  <div class="confirmation-overlay" v-if="show" @click.self="onCancel">
    <div class="confirmation-dialog">
      <div class="dialog-content" v-if="!isSuccess && !isError">
        <h3>{{ title }}</h3>
        <p>{{ message }}</p>
      </div>
      <div class="dialog-content success" v-else-if="isSuccess">
        <div class="success-icon">✓</div>
        <h3>接单成功</h3>
        <p>订单已添加到您的待配送列表</p>
      </div>
      <div class="dialog-content error" v-else>
        <div class="error-icon">!</div>
        <h3>无法接单</h3>
        <p>不能接取自己发布的订单</p>
      </div>
      <div class="dialog-buttons" v-if="!isSuccess && !isError">
        <button class="cancel-btn" @click="onCancel">取消</button>
        <button class="confirm-btn" @click="handleConfirm">确认</button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()
const isSuccess = ref(false)
const isError = ref(false)

const props = defineProps({
  show: Boolean,
  title: {
    type: String,
    default: '确认'
  },
  message: {
    type: String,
    default: '是否确认接取该订单？'
  },
  delegationId: {  
    type: [String, Number],
    required: true
  },
  senderId: {  // 添加 senderId 属性
    type: [String, Number],
    required: true
  }
})

const emit = defineEmits(['update:show', 'confirm', 'cancel'])

const handleConfirm = () => {
  const currentUser = JSON.parse(localStorage.getItem('user'))
  const currentUid = currentUser?.uid
  
  // 使用传入的 senderId 进行判断
  if (currentUid == parseInt(props.senderId)) {
    isError.value = true
    setTimeout(() => {
      isError.value = false
      emit('cancel')
      emit('update:show', false)
    }, 1500)
    return
  }

  isSuccess.value = true
  // 显示成功状态 1.5 秒后关闭弹窗
  setTimeout(() => {
    isSuccess.value = false
    emit('confirm')
    emit('update:show', false)
    // 跳转到详情页
    router.push(`/delegation/${props.delegationId}`)
  }, 1500)
}

const onCancel = () => {
  isSuccess.value = false
  isError.value = false
  emit('cancel')
  emit('update:show', false)
}
</script>

<style scoped>
.confirmation-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 2000;
}

.confirmation-dialog {
  background: white;
  border-radius: 15px;
  padding: 20px;
  width: 80%;
  max-width: 300px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.15);
}

.dialog-content {
  text-align: center;
  margin-bottom: 20px;
}

.dialog-content h3 {
  margin-bottom: 10px;
  color: #333;
}

.dialog-content p {
  color: #666;
  font-size: 14px;
}

.dialog-buttons {
  display: flex;
  justify-content: space-around;
  gap: 15px;
}

.dialog-buttons button {
  flex: 1;
  padding: 8px 0;
  border-radius: 20px;
  border: none;
  font-size: 14px;
  cursor: pointer;
}

.cancel-btn {
  background-color: #f5f5f5;
  color: #666;
}

.confirm-btn {
  background-color: #a5b2e5;
  color: white;
}

.success {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px 0;
}

.success-icon {
  width: 50px;
  height: 50px;
  background-color: #a5b2e5;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 24px;
  margin-bottom: 15px;
}

.success h3 {
  color: #a5b2e5;
  margin-bottom: 10px;
}

.success p {
  color: #666;
  font-size: 14px;
}

.error {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px 0;
}

.error-icon {
  width: 50px;
  height: 50px;
  background-color: #ff6b6b;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 24px;
  margin-bottom: 15px;
}

.error h3 {
  color: #ff6b6b;
  margin-bottom: 10px;
}

.error p {
  color: #666;
  font-size: 14px;
}
</style>
